home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 7 / BBS in a Box - Macintosh - Volume VII (BBS in a Box) (January 1993).iso / Files / Prog / M / MacOberon241.cpt / MacOberon 2.4(1) / Modula.Text (.txt) < prev    next >
Oberon Text  |  1992-01-06  |  6KB  |  94 lines

  1. Syntax10.Scn.Fnt
  2. ParcElems
  3. Alloc
  4. Syntax16.Scn.Fnt
  5. Syntax12.Scn.Fnt
  6. Syntax12b.Scn.Fnt
  7. Syntax12i.Scn.Fnt
  8. A Translator from Modula-2 to Oberon
  9. N. Wirth  4.1.92
  10. Program Modula is a translation aid for converting Modula-2 programs to Oberon. We emphasize the word aid. It is evidently not possible to perform such a conversion fully automatically, because Oberon differs from Modula-2 not only in its syntax, but also in various concepts, such as the command. However, the converter will be a valuable aid to perform a first step from which a genuine conversion taking into account the conceptual differences may start. The inputs to the converter are assumed to be correct Modula-2 modules.
  11. The following translations are made by the converter:
  12. 1. Definition and implementation modules are merged into a single Oberon module. Declaractions in the definition part are marked by an asterisk.
  13. 2. Import lists of the form
  14. FROM M IMPORT x, y
  15. are converted to IMPORT M. Occurences of x and y are subsequently converted to M.x and M.y.
  16. 3. Enumeration type declarations of the form
  17. Enum = (A, B)
  18. are translated to
  19. Enum = INTEGER;
  20. CONST A = 0; B = 1;
  21. 4. Subrange declarations of the form [m .. n] and T[ m .. n] are converted to INTEGER;
  22. 5. CARDINAL is translated into INTEGER.
  23. 6. Set types of the form SET OF T are translated into SET.
  24. 7. BITSET is translated into SET.
  25. 8. Type transfer functions are suppressed, i.e. T(exp) becomes exp. This may lead to type errors in the resulting program.
  26. 9. The symbols AND, NOT, and <> are converted to  &, ~, and # respectively.
  27. 10. Octal numbers are converted into hexadecimal notation.
  28. The following example shows the various translations:
  29. DEFINITION MODULE M2X1;
  30.     VAR X: INTEGER;
  31.     PROCEDURE P(u, v: INTEGER);
  32.     PROCEDURE Q(w: INTEGER): REAL;
  33. END M2X1.
  34. IMPLEMENTATION MODULE M2X1;
  35.     IMPORT M0;
  36.     FROM M1 IMPORT a, b;
  37.     FROM M2 IMPORT Read, Write;
  38.     CONST N = 100;
  39.     TYPE Color = (red, green, blue);
  40.         Interval = [1 .. 10];
  41.         Index = CARDINAL [0 .. 99];
  42.         Hue = SET OF Color;
  43.     VAR i: CARDINAL; x: REAL; h: BITSET; ch: CHAR;
  44.     PROCEDURE P(u, v: INTEGER);
  45.     BEGIN Read(X); Write(u+v); Write(u-v)
  46.     END P;
  47.     PROCEDURE Q(w: INTEGER): REAL;
  48.         VAR a: REAL; b: CARDINAL;
  49.     BEGIN a := REAL(w); b := CARDINAL(w+1); RETURN w
  50.     END Q;
  51.     PROCEDURE R(c: Color);
  52.         VAR p, q, r: BOOLEAN;
  53.     BEGIN IF c <> green THEN p := p AND q; q := NOT r END
  54.     END R;
  55. BEGIN a := M0.x; X := 443B; X := 0FFFH; ch := 33C; ch := 377C; h := {}
  56. END M2X1.
  57. The resulting translation is:
  58.  MODULE M2X1;
  59.     VAR X*: INTEGER;
  60.     IMPORT M0, M1, M2;    
  61. CONST N = 100;
  62.     TYPE Color = INTEGER;
  63.     CONST red = 0; green = 1; blue = 2;
  64.         Interval = INTEGER;
  65.         Index = INTEGER;
  66.         Hue = SET;
  67.     VAR i: INTEGER; x: REAL; h: SET; ch: CHAR;
  68.     PROCEDURE P*(u, v: INTEGER);
  69.     BEGIN M2.Read(X); M2.Write(u+v); M2.Write(u-v)
  70.     END P;
  71.     PROCEDURE Q*(w: INTEGER): REAL;
  72.         VAR a: REAL; b: INTEGER;
  73.     BEGIN a := w; b := w+1; RETURN w
  74.     END Q;
  75.     PROCEDURE R(c: Color);
  76.         VAR p, q, r: BOOLEAN;
  77.     BEGIN IF c # green THEN p := p & q; q := ~ r END
  78.     END R;
  79. BEGIN M1.a := M0.x; X := 123H; X := 0FFFH; ch := 1BX; ch := 0FFX; h := {}
  80. END M2X1.
  81. The result is not necessarily a correct Oberon program. Even in the example above, the import list needs to be moved ahead of the variable declaration. In particular the Modula WITH statement is not handled and needs to be converted "by hand". Also, the converter does not perform type checks.
  82. The converter translates modules independently. This means that imported identifiers are not verified as being exports of the imported modules.
  83. How to use the converter
  84. Converting a main module:
  85. 1. Open a viewer with the source text, say M2X0.MOD
  86. 2. Mark the viewer and do Modula.Convert
  87. A new viewer is opened (at the position of the mark) with the result text named M2X0.Mod. (The text is not yet stored as a file).
  88. Converting a definition and implementation pair:
  89. 1. Open a viewer with the definition part, say M2X1.DEF
  90. 2. Open a viewer with the implementation part, say M2X1.MOD
  91. 3. Mark the first viewer and do Modula.Convert
  92. 4. Mark the second viewer and do Modula.Convert
  93. A new viewer is opened with the result text named M2X1.Mod.
  94.